<?php

/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Socialengineaddon
 * @copyright  Copyright 2009-2010 BigStep Technologies Pvt. Ltd.
 * @license    http://www.socialengineaddons.com/license/
 * @version    $Id: IndexController.php (var) 2010-11-18 9:40:21Z SocialEngineAddOns $
 * @author     SocialEngineAddOns
 */
class Socialengineaddon_CoreController extends Core_Controller_Action_Standard {
	
	// ACTION FOR DOWNLOAD
	public function downloadAction() {
		//GET PATH
		$path = urldecode ( $_GET ['path'] );
		
		$path = preg_replace ( '/\.{2,}/', '.', $path );
		$path = preg_replace ( '/[\/\\\\]+/', '/', $path );
		$path = trim ( $path, './\\' );
		
		if (! Engine_Api::_ ()->socialengineaddon ()->isCdn ()) {
			$schema = 'http://';
			if (! empty ( $_ENV ["HTTPS"] ) && 'on' == strtolower ( $_ENV ["HTTPS"] )) {
				$schema = 'https://';
			}
			$host = $_SERVER ['HTTP_HOST'];
			$hostUrl = $schema . $host;
			$path = $hostUrl . '/' . $path;
		}
		
		$explodePath = explode ( "?", $path );
		$path = $explodePath ['0'];
		while ( ob_get_level () > 0 ) {
			ob_end_clean ();
		}
		header ( "Content-Disposition: attachment; filename=" . urlencode ( basename ( $path ) ), true );
		header ( "Content-Transfer-Encoding: Binary", true );
		header ( "Content-Type: application/force-download", true );
		header ( "Content-Type: application/octet-stream", true );
		header ( "Content-Type: application/download", true );
		header ( "Content-Description: File Transfer", true );
		header ( "Content-Length: " . filesize ( $path ), true );
		flush ();
		
		$fp = fopen ( $path, "r" );
		while ( ! feof ( $fp ) ) {
			echo fread ( $fp, 65536 );
			flush ();
		}
		fclose ( $fp );
		
		exit ();
	}

}

?>